package handlers

import (
	"github.com/gin-gonic/gin"
	"xiao-hong-book/models"
)

func Add(c *gin.Context) {
	username := c.PostForm("username")
	title := c.PostForm("title")
	content := c.PostForm("content")
	if username == "" || title == "" || content == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段为空",
		})
		return
	}
	data := models.Article{
		Username: username,
		Title:    title,
		Content:  content,
	}
	err := models.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func List(c *gin.Context) {
	var article []models.Article
	models.DB.Debug().Find(&article)
	c.JSON(200, gin.H{
		"code": 200,
		"data": article,
		"msg":  "列表成功",
	})
	return
}
